<?php
/**
* @package mosRest
* @author Chad Auld and Ozgur Cem Sen (code@brilaps.com)
* @copyright Brilaps, LLC (http://brilaps.com)
* @link http://brilaps.com || http://wiki.brilaps.com
* @license http://www.opensource.org/licenses/gpl-license.php GNU/GPL v.2.
*/

/** ensure this file is being included by a parent file */
defined( '_VALID_MOS' ) or die( 'Direct Access to this location is not allowed.' );

//Fix to allow compatibility with J!
if (!function_exists( 'T_' )) { function T_($string) {return $string;} }

class HTML_registration {
	
	function lostKeyForm($option) {
	?>

	<h2 class="componentheading"><?php echo T_('Lost your API key?'); ?></h2>
	  <form action="index.php" method="post">
	  <div id="register-lost-key">
		<p><?php echo T_('Please enter the e-mail address you registered API access with and then click on the API Reset Request button.'); ?></p>
		<p><label for="confirmEmail"><?php echo T_('Contact E-mail Address:'); ?></label><br />
		<input type="text" id="confirmEmail" name="confirmEmail" class="inputbox" size="40" /></p>
		 <input type="hidden" name="option" value="<?php echo $option;?>" />
	        <input type="hidden" name="task" value="sendNewKey" /> 
	        <p><input type="submit" class="button" value="<?php echo T_('API Reset Request'); ?>" /></p>
	   </div><!--register-lost-key-->
	  </form>
  
	<?php
	}

	function registerForm($option) {
		global $mainframe, $mosConfig_absolute_path;

		//Include REST settings
		$show_rest_registration_disclaimer = '';
		$rest_registration_disclaimer_message = '';
		require($mosConfig_absolute_path.'/administrator/components/com_rest/config.rest.php');
		
		$sitename = $mainframe->getCfg( 'sitename' );
		$reg_developer_name = trim(mosGetParam( $_REQUEST, 'developer_name', ''));
		$reg_product_name = trim(mosGetParam( $_REQUEST, 'product_name', ''));
		$reg_web_app_url = trim(mosGetParam( $_REQUEST, 'web_app_url', ''));
		$reg_contact_email = trim(mosGetParam( $_REQUEST, 'contact_email', ''));
		$reg_phone_number = trim(mosGetParam( $_REQUEST, 'phone_number', ''));
		$reg_description = trim(mosGetParam( $_REQUEST, 'description', ''));

		//Holds an error message returned from the form validation process
		$reg_error_type = trim(mosGetParam( $_SESSION, 'reg_error_type', '' ));
		$reg_error_text = trim(mosGetParam( $_SESSION, 'reg_error_text', '' ));
		?>

		<h2 class="componentheading">	<?php echo $sitename.' '.T_('API Registration'); ?></h2>
		<h3><?php echo T_('Enter registration information'); ?></h3>
		<?php
		//Output an returning validation error messages
		if ($reg_error_type!='') {
			echo "<p style=\"color: red;font-weight: bold;\">$reg_error_text</p>";
		}
		?>
		<form action="index.php" method="post" id="registrationForm">
		<div id="register-new">
	
		<p><?php echo T_('Fields marked with an asterisk (*) are required.'); ?></p>
			<p><label for="developer_name"><?php echo T_('Developer Name:'); ?></label> *<br />
			<input type="text" id="developer_name" name="developer_name" size="40" class="<?php echo ($reg_error_type=='developer_name' ? "registrationerror" : "inputbox"); ?>" value="<?php echo $reg_developer_name; ?>" /></p>
		    <p><label for="product_name"><?php echo T_('Product Name:'); ?></label> *<br />
			<input type="text" id="product_name" name="product_name" size="40" class="<?php echo ($reg_error_type=='product_name' ? "registrationerror" : "inputbox"); ?>" value="<?php echo $reg_product_name; ?>" /></p>
			<p><label for="web_app_url"><?php echo T_('Web Application URL:'); ?></label> <br />
			<input type="text" id="web_app_url" name="web_app_url" size="40" class="<?php echo ($reg_error_type=='web_app_url' ? "registrationerror" : "inputbox"); ?>" value="<?php echo $reg_web_app_url; ?>" /></p>
			<p><label for="contact_email"><?php echo T_('Contact Email:'); ?></label> *<br />
			<input type="text" id="contact_email" name="contact_email" size="40" class="<?php echo ($reg_error_type=='contact_email' ? "registrationerror" : "inputbox"); ?>" value="<?php echo $reg_contact_email; ?>" /></p>
			<p><label for="phone_number"><?php echo T_('Phone Number:'); ?></label> <br />
			<input type="text" id="phone_number" name="phone_number" size="40" class="<?php echo ($reg_error_type=='phone_number' ? "registrationerror" : "inputbox"); ?>" value="<?php echo $reg_phone_number; ?>" /></p>
			<p><label><?php echo T_('Description:'); ?></label> *<br />
			<textarea name="description" rows="5" cols="40" class="<?php echo ($reg_error_type=='description' ? "registrationerror" : "inputbox"); ?>"><?php echo $reg_description; ?></textarea></p>
			<?php 
		    if ($show_rest_registration_disclaimer=='1') {
		    ?>
		    <p><label for="privacypolicy"><?php echo T_('Disclaimer and Privacy Policy:'); ?></label> *<br />
		     <textarea name="privacypolicy" cols="48" rows="4" id="privacypolicy" class="<?php echo ($reg_error_type=='accept' ? "registrationerror" : "inputbox"); ?>" style="font-size:0.85em;" readonly><?php echo $rest_registration_disclaimer_message; ?></textarea></p>
		     <p><input name="accept" type="checkbox" id="accept" value="yes" />        
		        <label for="accept"><?php echo T_('Yes, I Accept'); ?></label></p>
		    <?php 
		    }
		?>

	<input type="hidden" name="id" value="0" />
	<input type="hidden" name="gid" value="0" />
	<input type="hidden" name="option" value="<?php echo $option; ?>" />
	<input type="hidden" name="task" value="confirmRegistration" />
	<p><input type="submit" name="send" value="<?php echo T_('Send Registration'); ?>" class="button" /></p>
	</div><!--register-new-->
	</form>
	<?php
		/*  If there was an error and session values were used to restore the users submitted form then go ahead and clear 
		the session values now that form is ready so as not to incorrectly restore them if the user returns to
		this form at a later time aftering bailing out of the regisitration form without successful submit.  They will be reset 
		if another error occurs.  */		
		if ($reg_error_type!='') { 
			unset($_SESSION['reg_error_type']);
			unset($_SESSION['reg_error_text']);
			unset($_SESSION['reg_developer_name']);
			unset($_SESSION['reg_product_name']);
			unset($_SESSION['reg_web_app_url']);
			unset($_SESSION['reg_contact_email']);
			unset($_SESSION['reg_phone_number']);
			unset($_SESSION['reg_description']);
		}  //end session unset
	}


	function confirmForm($option, $developer_name, $product_name, $web_app_url, $contact_email, $phone_number, $description) {
		global $mainframe;
		$sitename = $mainframe->getCfg( 'sitename' );
	?>
	<script type="text/javascript">
	function reviseData() {
		var form = document.mosForm;
		form.task.value='reviseRegistration';
		form.submit();
	}
	</script>
	<h2 class="componentheading"><?php echo $sitename.' '.T_('API Registration'); ?></h2>
	<h3><?php echo T_('Data confirmation'); ?></h3>
	<form action="index.php" method="post" name="mosForm">
	<div id="register-confirm">
	<p><?php echo T_('Please verify that the following data is correct and click the button below to complete registration.'); ?> </p>
	<dl>
		<dt><?php echo T_('Developer Name:'); ?></dt>
		<dd><?php echo $developer_name;?><input type="hidden" name="developer_name" size="40" value="<?php echo $developer_name;?>" /></dd>
		<dt><?php echo T_('Product Name:'); ?> </dt>
		<dd><?php echo $product_name;?><input type="hidden" name="product_name" size="40" value="<?php echo $product_name;?>" /></dd>
		<dt><?php echo T_('Application Url:'); ?> </dt>
		<dd><?php echo !empty($web_app_url) ? $web_app_url : '<br />';?><input type="hidden" name="web_app_url" size="40" value="<?php echo $web_app_url;?>" /></dd>
		<dt><?php echo T_('Contact E-mail:'); ?> </dt>
		<dd><?php echo $contact_email;?><input type="hidden" name="contact_email" size="40" value="<?php echo $contact_email;?>" /></dd>
		<dt><?php echo T_('Phone Number:'); ?> </dt>
		<dd><?php echo !empty($phone_number) ? $phone_number : '<br />';?><input type="hidden" name="phone_number" size="40" value="<?php echo $phone_number;?>" /></dd>
	</dl>
   
	   <div class="register-confirm">
	   <p><?php echo T_('Go back and correct your data.'); ?></p>
	   <p><input name="back" type="button" class="button" id="back" value="<?php echo T_('Correct Your Data'); ?>" onclick="reviseData()" /></p>
	   </div><!--register-confirm-->
   
	   <div class="register-confirm">
	   <p><?php echo T_('The data is correct.'); ?></p>
	   <p><input name="confirm" type="submit" class="button" id="confirm" value="<?php echo T_('Confirm Registration'); ?>"/></p>
	   </div><!--register-confirm-->
      
	<input type="hidden" name="id" value="0" />
	<input type="hidden" name="gid" value="0" />
	<input type="hidden" name="option" value="<?php echo $option; ?>" />
	<input type="hidden" name="description" value="<?php echo $description; ?>" />
	<input type="hidden" name="task" value="saveRegistration" />
	</div><!--register-confirm-->
	</form>
	<?php
	}

}
?>
